<template>
  <div class="app-container">

    <h2 class="text-center">{{ paperData.title }}</h2>
    <p class="text-center" style="color: #666">{{ formatter2(paperData.createTime) }}</p>

    <el-row :gutter="24" style="margin-top: 50px">

      <el-col :span="8" class="text-center">
        考生姓名：{{ paperData.username }}
      </el-col>

      <el-col :span="8" class="text-center">
        考试用时：{{ paperData.userTime }}分钟
      </el-col>

      <el-col :span="8" class="text-center">
        考试得分：{{ paperData.userScore }}
      </el-col>

    </el-row>

    <el-card style="margin-top: 20px">

      <div v-for="item in paperData.quList" class="qu-content">

        <p>
          <el-row :gutter="24">
            <el-col :span="12">
              {{ item.sort + 1 }}.{{ item.content }}（得分：{{ item.actualScore }}）
            </el-col>
            <el-col :span="12" style="text-align: right; ">
              您的评分：<el-input-number v-model="scores[item.id]" :min="0" :max="item.score" :step="1" :precision="0" />
            </el-col>
          </el-row>
        </p>
        <div v-if="item.remark!=''&&item.remark!=null" class="qu-remark">
          {{ quData.remark }}}
        </div>
        <div v-if="item.coding!=''&&item.coding!=null" class="qu-coding">
          <codemirror :value="item.coding" :options="codingOptions" />
        </div>
        <div v-if="item.questionType === 1 || item.questionType===3">
          <el-radio-group v-model="radioValues[item.id]">
            <el-radio v-for="an in item.answerList" :label="an.id">{{ an.abc }}.{{ an.content }}</el-radio>
          </el-radio-group>

          <el-row :gutter="24">

            <el-col :span="12" style="color: #24da70">
              正确答案：{{ radioRights[item.id] }}
            </el-col>

            <el-col v-if="!item.answered" :span="12" style="text-align: right; color: #ff0000;">
              答题结果：未答
            </el-col>

            <el-col v-if="item.answered && !item.isRight" :span="12" style="text-align: right; color: #ff0000;">
              答题结果：{{ myRadio[item.id] }}
            </el-col>

            <el-col v-if="item.answered && item.isRight" :span="12" style="text-align: right; color: #24da70;">
              答题结果：{{ myRadio[item.id] }}
            </el-col>

          </el-row>

        </div>

        <div v-if="item.questionType === 100">

          <el-row :gutter="24">

            <el-col :span="12" style="color: #24da70">
              学生答案：
              <codemirror :value="item.answer" :options="qaOptions" />
            </el-col>

          </el-row>

        </div>

        <div v-if="item.questionType === 101">

          <el-row :gutter="24">

            <el-col :span="12" style="color: #24da70">
              学生答案：
              <codemirror :value="item.answer" :options="qaOptions" />
            </el-col>

          </el-row>

        </div>

        <div v-if="item.questionType === 2">
          <el-checkbox-group v-model="multiValues[item.id]">
            <el-checkbox v-for="an in item.answerList" :label="an.id">{{ an.abc }}.{{ an.content }}</el-checkbox>
          </el-checkbox-group>

          <el-row :gutter="24">

            <el-col :span="12" style="color: #24da70">
              正确答案：{{ multiRights[item.id].join(',') }}
            </el-col>

            <el-col v-if="!item.answered" :span="12" style="text-align: right; color: #ff0000;">
              答题结果：未答
            </el-col>

            <el-col v-if="item.answered && !item.isRight" :span="12" style="text-align: right; color: #ff0000;">
              答题结果：{{ myMulti[item.id].join(',') }}
            </el-col>

            <el-col v-if="item.answered && item.isRight" :span="12" style="text-align: right; color: #24da70;">
              答题结果：{{ myMulti[item.id].join(',') }}
            </el-col>

          </el-row>
        </div>

      </div>

    </el-card>
    <el-row>
      <el-col>
        <br>
        <el-button type="primary" icon="el-icon-plus" @click="submit">提交阅卷打分</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

import { reviseResult, revisePaper } from '@/api/paper/exam'
import { dateTimeFormatter } from '@/utils/date'
import 'codemirror/theme/darcula.css'
import 'codemirror/mode/clike/clike'

export default {
  name: 'HandleExamDetail',
  data() {
    return {
      // 试卷ID
      paperId: '',
      paperData: {
        quList: []
      },
      radioValues: {},
      multiValues: {},
      radioRights: {},
      multiRights: {},
      myRadio: {},
      myMulti: {},
      qaOptions: {
        mode: 'text/x-java',
        lineNumbers: true,
        readOnly: true,
        viewportMargin: Infinity
      },
      codingOptions: {
        mode: 'text/x-java',
        readOnly: true,
        lineNumbers: true,
        viewportMargin: Infinity
      },
      scores: {},
      scoresTemp: {}
    }
  },
  created() {
    const id = this.$route.params.id
    if (typeof id !== 'undefined') {
      this.paperId = id
      this.fetchData(id)
    }
  },
  methods: {
    submit() {
      const arrs = []
      let key = ''
      let count = 0
      for (key in this.scores) {
        const sco = this.scores[key]
        count += sco
        arrs.push({
          'id': key,
          'actualScore': sco,
          'isRight': (sco > 0 && sco == this.scoresTemp[key])
        })
      }
      if (window.confirm('学生共计得分【' + count + '】,确定要提交？')) {
        const params = {
          'id': this.paperId,
          'quList': arrs
        }
        revisePaper(params).then(response => {
          this.$message.success('操作成功！')
          this.$router.push({ name: 'ShowExam', params: { id: this.paperId }})
        })
      }
    },
    formatter2(time) {
      return dateTimeFormatter(time)
    },
    fetchData(id) {
      const params = { id: id }
      reviseResult(params).then(response => {
        // 试卷内容
        this.paperData = response.data
        // 填充该题目的答案
        this.paperData.quList.forEach((item) => {
          let radioValue = ''
          let radioRight = ''
          let myRadio = ''
          const multiValue = []
          const multiRight = []
          const myMulti = []
          let stemp = item.actualScore
          if (!item.isRight) {
            stemp = 0
          }
          this.$set(this.scores, item.id, stemp)
          this.$set(this.scoresTemp, item.id, stemp)
          item.answerList.forEach((an) => {
            // 用户选定的
            if (an.checked) {
              if (item.questionType === 1 || item.questionType === 3) {
                radioValue = an.id
                myRadio = an.abc
              } else {
                multiValue.push(an.id)
                myMulti.push(an.abc)
              }
            }

            // 正确答案
            if (an.isRight) {
              if (item.questionType === 1 || item.questionType === 3) {
                radioRight = an.abc
              } else {
                multiRight.push(an.abc)
              }
            }
          })
          this.multiValues[item.id] = multiValue
          this.radioValues[item.id] = radioValue

          this.radioRights[item.id] = radioRight
          this.multiRights[item.id] = multiRight

          this.myRadio[item.id] = myRadio
          this.myMulti[item.id] = myMulti
        })
      })
    }
  }
}
</script>

<style>
  .qu-remark{
    margin: 18px;
    font-size: 10px;
    color: #666666;
    background-color: #adadad;
  }
  .qu-coding {
    margin: 18px;
  }
  .CodeMirror {
    border: 1px solid #eee;
    height: auto;
  }
</style>

<style scoped>

  .qu-content{

    border-bottom: #eee 1px solid;
    padding-bottom: 10px;

  }

  .qu-content div{
    line-height:30px;
  }

  .el-checkbox-group label,.el-radio-group label{
    width: 100%;
  }

  .card-title{
    background: #eee;
    line-height: 35px;
    text-align: center;
    font-size: 14px;
  }
  .card-line{
    padding-left: 10px
  }
  .card-line span {
    cursor: pointer;
    margin: 2px;
  }

</style>

